const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatDate = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  return [year, month, day].map(formatNumber).join('-');
}

function formatNumber(n) {

    n = n.toString()

    return n[1] ? n :'0'+ n

}
const formatDateToWeek = data => {
    var date = new Date(Date.parse(data));
    const month = date.getDay();
    var weekDay ;
    switch (month){
        case 0:
            weekDay = '周日';
            break;
        case 1:
            weekDay = '周一';
            break;
        case 2:
            weekDay = '周二';
            break;
        case 3:
            weekDay = '周三';
            break;
        case 4:
            weekDay = '周四';
            break;
        case 5:
            weekDay = '周五';
            break;
        case 6:
            weekDay = '周六';
            break
    }
    return weekDay;
}

function haveSomeMinutesTime (n) {
    if (n == null) {
      n = 0;
    }
    // 时间
    var newDate = new Date()
    // var timeStamp = newDate.getTime(); //获取时间戳
    var date = newDate.setMinutes(newDate.getMinutes() + n);
    newDate = new Date(date);
    var year = newDate.getFullYear();
    var month = newDate.getMonth() + 1;
    var day = newDate.getDate();
    var h = newDate.getHours();
    var m = newDate.getMinutes();
    var s = newDate.getSeconds();
    if (month < 10) {
      month = '0' + month;
    };
    if (day < 10) {
      day = '0' + day;
    };
    if (h < 10) {
      h = '0' + h;
    };
    if (m < 10) {
      m = '0' + m;
    };
    if (s < 10) {
      s = '0' + s;
    };
    var time = year +'/' + month+'/' + day +' '+ h+':' + m+':' + s;
    return time;
  }


/**
 * 日期加减
 * @param today null默认为当天时间
 * @param addDayCount
 * @returns {string}
 */
function getDayByDay(today, addDayCount) {
    var dd;
    if(today){
        dd = new Date(today);
    }else{
        dd = new Date();
    }
    dd.setDate(dd.getDate() + addDayCount);//获取AddDayCount天后的日期
    var y = dd.getFullYear();
    var m = dd.getMonth() + 1;//获取当前月份的日期
    var d = dd.getDate();
    if (m < 10) {
        m = '0' + m;
    };
    if (d < 10) {
        d = '0' + d;
    };
    return y + "-" + m + "-" + d;
}

function getDaysDiff(start_date,end_date) {
    var start_timestamp = Date.parse(start_date);
    var end_timestamp = Date.parse(end_date);
    return Math.abs(parseInt((end_timestamp-start_timestamp)/(24 * 60 * 60 * 1000)));
}


module.exports = {
  formatTime: formatTime,
  formatDate: formatDate,
  formatNumber: formatNumber,
  formatDateToWeek: formatDateToWeek,
  getDaysDiff: getDaysDiff,
  getDayByDay: getDayByDay,
  haveSomeMinutesTime:haveSomeMinutesTime,
};
